<?php

class Application_Model_Dao_User extends Zend_Db_Table_Abstract
{

	protected $_name = 'users';
	protected $_primary = 'id';

	public function init()
	{
		 
	}

	/*
	 *
	 * get
	 *
	 */

	public function getAll()
	{
		$result = $this->fetchAll();
		if( !$result ) {
			throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		}
		return $result;
	}

	public function getAllUser(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('users')->joinLeft('cathedrals', 'users.cathedral_id = cathedrals.id', array('cathedral_designation' => 'designation'))
		->joinLeft('roles', 'users.role_id = roles.id', array('role_designation' => 'designation'))
		->joinLeft('academic_degrees', 'users.academicDegree_id = academic_degrees.id', array('academicDegree_designation' => 'designation'));
			
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}

	public function getRowUser($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('users')->where('users.id = \''.$id.'\'')->joinLeft('cathedrals', 'users.cathedral_id = cathedrals.id', array('cathedral_designation' => 'designation'))
		->joinLeft('roles', 'users.role_id = roles.id', array('role_designation' => 'designation'))
		->joinLeft('academic_degrees', 'users.academicDegree_id = academic_degrees.id', array('academicDegree_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}


	public function getAllLecturer(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('users')->where('users.lecturer = \'1\'')->joinLeft('cathedrals', 'users.cathedral_id = cathedrals.id', array('cathedral_designation' => 'designation'))
		->joinLeft('roles', 'users.role_id = roles.id', array('role_designation' => 'designation'))
		->joinLeft('academic_degrees', 'users.academicDegree_id = academic_degrees.id', array('academicDegree_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}


	public function getUserByID( $id )
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row) {
			throw new Exception("Użytkownik o ID $id nie istnieje.");
		}
		return $row->toArray();
	}

	public function getToSelect(){
		$array = $this->getAll()->toArray();
		$users[''] = "wybierz";
		foreach ($array as $option) {
			//$degreeStudies[ $option['id'] ] = $option['designation' ] ;
		}
		return $users;
	}

	/**
	 *
	 * Zwraca wszystkich użytkowników systemu, którzy są wykładowcami
	 */
	public function getToSelectLecturers() {
		try {
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = 'SELECT u.id, u.name, u.surname, ad.designation as academic_degree, c.designation as cathedral
				FROM users as u
				INNER JOIN academic_degrees as ad
				INNER JOIN cathedrals as c 
				WHERE u.lecturer=1 ';
			$stmt = $db->query($sql);
		} catch (Exception $exception) {
			Zend_Registry::get('logger')->log('Brak wykładowców. Exception: '.$exception, Zend_Log::NOTICE);
		}

		$users[''] = "wybierz";
		 
		while ($row = $stmt->fetch()) {
			$users[ $row['id'] ] = $row['academic_degree']." ".$row['name']." ".$row['surname']." (".$row['cathedral'].")" ;

		}
		return $users;
	}

	/*
	 * save
	 *
	 */

	public function saveUser($user) {
		$data = array(
			'academicDegree_id' => $user->getAcademicDegree(),
			'name' => $user->getName(),
    		'surname' => $user->getSurname(),
    		'email' => $user->getEmail(),
    		'password' => $user->hashPass($user->getPassword(), $this->hashHashcode($user->getEmail())),
    		'hashcode' => $this->hashHashcode($user->getEmail()),
    		'cathedral_id' => $user->getCathedral(),
    		'role_id' => $user->getRole(),
    		'lecturer' => $user->getLecturer(),
		);
		$result = $this->insert($data);
		return $result;
	}

	/*
	 *
	 * update
	 *
	 */

	public function updateUser($user) {
		$data = array(
			'academicDegree_id' => $user->getAcademicDegree(),
			'name' => $user->getName(),
    		'surname' => $user->getSurname(),
    		'email' => $user->getEmail(),
    		'password' => $user->hashPass($user->getPassword(), $this->hashHashcode($user->getEmail())),
    		'hashcode' => $this->hashHashcode($user->getEmail()),
    		'cathedral_id' => $user->getCathedral(),
    		'role_id' => $user->getRole(),
			'lecturer' => $user->getLecturer(),
		);
		$result = $this->update($data, 'id = '. (int)$user->getId());
		return $result;
	}
	public function deleteUser($user){
		$result = $this->delete('id =' . (int)$user->getId());
		return $result;
	}

	public function updatePassword($email, $password)
	{
		$data = array(
		'password' => $password,
		//'password' => "e9a0e7c50651c51e8d3717aa6939aa822c694964",
		);
		$this->update($data, 'email = \''. (string)$email.'\'');
	}
	public function getHashcode($email){
		$email = (string)$email;
		$row = $this->fetchRow('email = \'' . $email.'\'');
		if (!$row) {
			return null;
			//throw new Exception("Could not find row $email");
		}
		return $row['hashcode'];
		//return $row->toArray();

	}
	public function hashHashcode($hashcode){
		return SHA1($hashcode);
	}
}
 